import Vue from 'vue'
import Vuex from 'vuex'
import products from './modules/products'
import cart from './modules/cart'

Vue.use(Vuex)

const saveCartPlugin = store => {
  store.subscribe((mutation, state) => {
    if (mutation.type.startsWith('cart/') && state.cart.cartProducts.length > 0) {
      window.localStorage.setItem('cart-products', JSON.stringify(state.cart.cartProducts))
    }
  })
}

export default new Vuex.Store({
    state: {
    },
    mutations: {
    },
    actions: {
    },
    modules: {
      products,
      cart
    },
    plugins: [saveCartPlugin]
})